package com.day1018.UDP_Socket;

import java.io.IOException;
import java.io.StringReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketAddress;
import java.net.SocketException;

public class MainText {
    public static void main(String[] args) throws IOException {
        DatagramSocket datagramSocket = new DatagramSocket(8888);
        byte[] bytes = new byte[1024];
        DatagramPacket datagramPacket = new DatagramPacket(bytes,bytes.length);
        System.out.println("等待接收数据**********");
        while (true) {
            datagramSocket.receive(datagramPacket);
            String str = new String(datagramPacket.getData(), 0, datagramPacket.getLength());
            System.out.println(datagramPacket.getAddress() + ":" + datagramPacket.getPort() + " 发送信息:" + str);

            String serverinfo = "我是服务端";
            SocketAddress sa = datagramPacket.getSocketAddress();
            DatagramPacket serverPacket = new DatagramPacket(serverinfo.getBytes(), 0, serverinfo.getBytes().length, sa);
            datagramSocket.send(serverPacket);
            //datagramSocket.receive(datagramPacket);
            //String info = new String(datagramPacket.getData(),0,datagramPacket.getLength());
            //System.out.println("从客户端接收到的值是：" + info);
        }
    }
}
